package com.panshi.common;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;


import com.panshi.api.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.HashMap;

/**
 * @author :jiege
 * @description :
 * @create :2020-08-03 14:47:00
 */
@Component
public class EmailPhoneUtill {

    @Autowired
    private JavaMailSenderImpl JavaMailSenderImpl;

    @Value("${spring.mail.username}")
    private String myemail;

    @Value("${server.servlet.session.timeout}")
    private Integer timeout;

    @Autowired
    private RedisUtil redisUtil;//redis

    //发送邮件
    public void getemail(String email) throws MessagingException {
        //一个复杂的邮件
        MimeMessage mimeMessage=JavaMailSenderImpl.createMimeMessage();
        //组装
        MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);
        helper.setSubject("渣男团的有贝口腔");
        int num=(int)((Math.random()*9+1)*100000);
        redisUtil.set(email+":"+num,num,timeout);
        helper.setText("<p style='color:red'>你好</p><p>你在渣男团的有贝口腔的验证码如下:</p>【"+num+"】<p>如果不是你获取的验证码轻忽略本邮件 谢谢</p>",true);//true支持html
        helper.setTo(email);
        helper.setFrom(myemail);
        JavaMailSenderImpl.send(mimeMessage);
    }
    //发送电话号码
    public void getiphone(String iphone) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G3KK55cMp1LZdJ1f7kM", "Z9IuUnRlkxFVCU2aMCls2dODMYu2Sp");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", iphone);
        request.putQueryParameter("SignName", "ABC商城");
        request.putQueryParameter("TemplateCode", "SMS_197886003");
        HashMap<String, Object> hashMap = new HashMap<>();
        int num=(int)((Math.random()*9+1)*100000);
        hashMap.put("code",num);
        redisUtil.set(iphone+":"+num,num,timeout);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(hashMap));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    //发送邮件审核通过表发送邮箱提示
    public void setemailarticle(String email,String title) throws MessagingException {
        //一个复杂的邮件
        MimeMessage mimeMessage=JavaMailSenderImpl.createMimeMessage();
        //组装
        MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);
        helper.setSubject("渣男团的有贝口腔");
        int num=(int)((Math.random()*9+1)*100000);
        redisUtil.set(email+":"+num,num,timeout);
        helper.setText(title,true);//true支持html
        helper.setTo(email);
        helper.setFrom(myemail);
        JavaMailSenderImpl.send(mimeMessage);
    }


}
